<?php
use PFT\Tool\Tools;
use PFT\PriceGroup;
use PFT\Db;

//TODO:登陆判断

include 'class/Tools.class.php';
include 'class/PriceGroup.class.php';
include '/var/www/html/new/d/module/common/Db.class.php';
include '/var/www/html/new/d/class/abc/PFTCoreAPI.class.php';
$db_conf = include '/var/www/html/new/d/module/common/db.conf.php';
Db::ConnectRef();

$soap_client = new SoapClient(null,array(
	"location" => "http://localhost/open/openService/pft_insideMX.php",
	"uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
);

$req = Tools::deepRemoveXss($_POST);
$sid = $_SESSION['sid'];
switch ($req['act']) {
	case 'test':
		PriceGroup::test();
		break;
	//获取供应商的产品
	case 'get_products':
		if (!isset($req['for']) || !$req['for']) {
			ajaxReturn('paramError');
		}
		if ($req['for'] == 'distributor') {
			if (!isset($req['did']) || !$req['did']) {
				ajaxReturn('paramError');
			}
		} else {
			if (!isset($req['gid']) || !$req['gid']) {
				ajaxReturn('paramError');
			}
			$group_info = PriceGroup::getGroupById($req['gid'], $sid);
			if (!$group_info) {
				ajaxReturn('paramError');
			}
		}
		$page = isset($req['page']) && $req['page'] ? $req['page'] : 1;
		$page_size = isset($req['page_size']) && $req['page_size'] ? $req['page_size'] : 10;
		$key_condition = '';
		if (isset($req['keyword']) && $req['keyword']) {
			$key_condition = " and instr(p.p_name,'".$req['keyword']."') ";
		}
		$limit = 'limit ' . ($page - 1) * $page_size . ',' . $page_size;
		$products_sql = "
			select p.id as pid,p.p_name,l.id as lid,t.id as tid,t.tprice,p.apply_did 
			from uu_products p left join uu_land l on p.contact_id=l.id 
			left join uu_jq_ticket t on p.id=t.pid 
			where p.apply_did={$sid} and p.p_status in (0,3,4,5) and p.apply_limit=1 {$key_condition} 
			order by l.id asc {$limit}";
		$products = Db::fetchAll($products_sql);

		$total_sql = "select count(p.id) as count
			from uu_products p left join uu_land l on p.contact_id=l.id 
			left join uu_jq_ticket t on p.id=t.pid 
			where p.apply_did={$sid} and p.p_status in (0,3,4,5) and p.apply_limit=1";
		$total = Db::fetch($total_sql);
		$total = $total['count'];

		//供应商没有发布产品TODO:这种情况测试
		if (count($products) < 1) {
			ajaxReturn('noProduct');
		}

		$did = $req['did'];
		if ($req['for'] == 'distributor') {
			$sale_list_sql = "select * from pft_product_sale_list where aid={$sid} and fid=$did limit 1";
			$sale_list = Db::fetch($sale_list_sql);
			if ($sale_list['pids'] == 'A') {
				$allow_pids = 'A';
			}
			$allow_pids = explode(',', $sale_list['pids']);
			
		} else {
			// $gid = $req['gid'];
			// $group_info = PriceGroup::getGroupById($gid, $sid);
			if ($group_info['default_inc']) {
				$default_inc = json_decode($group_info['default_inc'], true);
				$allow_pids = array_keys($default_inc);
			} else {
				$allow_pids = array();
			}
		}
		foreach ($products as $key => $product) {
			if ($allow_pids == 'A') {
				$products[$key]['allow'] = true;
			}
			// $products[$key]['p_name'] = $product['title'] . $product['ttitle'];
			if (in_array($product['pid'], $allow_pids)) {
				$products[$key]['allow'] = true;
			} else {
				$products[$key]['allow'] = false;
			}
		}
		if ($req['for'] == 'distributor') {
			$sql = "select pid,tid,dprice from uu_priceset where pid={$did} and aid=0";
			$price_arr = Db::fetchAll($sql);
			$tmp = array();
			foreach ($price_arr as $price) {
				$tmp[$price['tid']] = $price['dprice'] / 100;
			}
			$price_arr = $tmp;
		}
		// var_dump($price_arr);die;
		foreach ($products as $key => $product) {
			$p_storage = PFTCoreAPI::pStorage(
				$soap_client, $_SESSION['saccount'], 
				$product['pid'], $sid, date('Y-m-d'),2		
				);
			$products[$key]['supply_price'] = $p_storage['js']['p'];
			$products[$key]['recom_price'] = $p_storage['ls']['p'];
			if ($req['for'] == 'distributor') {
				if ($p_storage['js']['p'] == -1 || $product['allow'] == false) {
					$products[$key]['settle_price'] = $p_storage['js']['p'];
				} else {
					if (isset($price_arr[$product['pid']])) {
						$products[$key]['settle_price'] = $p_storage['js']['p'] + $price_arr[$product['pid']];
					} else {
						$products[$key]['settle_price'] = $p_storage['ls']['p'];
					}
				}
				
			} else {
				if (isset($default_inc[$product['pid']])) {
					$products[$key]['settle_price'] = $default_inc[$product['pid']] / 100;
				} else {
					$products[$key]['settle_price'] = $p_storage['js']['p'];
				}
			}
		}
		if ($req['for'] == 'distributor') {
			$name = Db::fetch("select dname from pft_member where id={$req['did']}");
			$name = $name['dname'];
		} else {
			$name = $group_info['name'];
		}
		$result = array(
			'name' => $name,
			'data' => $products,
			'total_page' => ceil($total / $page_size),
			'current_page' => $page
		);
		ajaxReturn('productsList', $result);
		break;

	//获取转分销产品
	case 'get_dis_products':
		$did = $req['did'];
		// $prodname = $_REQUEST['prodname'];
		// 分销商名称
		$info_sql = "select dname,account from pft_member where id=$did limit 1";
		$info = Db::fetch($info_sql);
		$dname = $info['dname']; $daccount = $info['account'];

		$page_size = $req['page_size'] ? $req["page_size"] : 10;
		$page = $req["page"] ? $req["page"] : 1;
		$limit =  ($page - 1) * $page_size .','.$page_size;
		// 获取会员分销的产品
		$pids = $result = array();
		if (isset($req['keyword']) && $req['keyword']) {
			$key_condition = " and instr(p.p_name,'".$req['keyword']."') ";
		}
		$where = "where e.fid=$sid and e.status=0 and e.active=1 and lvl<3 
				 and l.status=1 and p.apply_limit=1 and p.p_status in (0,1) {$key_condition}";
		$product_sql = "select e.pid,p.p_name,e.sid,e.lvl,m.dname,e.aids 
				from `pft_p_apply_evolute` e left join uu_products p on e.pid=p.id 
				left join pft_member m on e.sid=m.id 
				left join uu_land l on p.contact_id=l.id 
				{$where} 
				order by p_name desc limit $limit";
		$products = Db::fetchAll($product_sql);
		foreach ($products as $row){
			$p_storage = PFTCoreAPI::pStorage(
				$soap_client, $_SESSION['saccount'], 
				$row['pid'], $row['sid'], date('Y-m-d')
			);
			if($p_storage['js']['p'] == -1) {
				continue;
			} 
			$pk = $row['pid'].'_'.$row['sid'];
		    $pids[$pk] = $row['pid'];
		    $result[$pk] = array(
		        'pid'  => $row['pid'],
				'supply_price'  => $p_storage['js']['p'],
				'recom_price'  => $p_storage['ls']['p'],
				'aid' => $row['sid'],
				'aids'=> $row['aids'],
				'dname'    => $row['dname'],
		        'p_name'   => $row['p_name'],
		        'allow' => false,
		    );
		}
		$total_sql = "select count(e.pid) as total 
				from `pft_p_apply_evolute` e left join uu_products p on e.pid=p.id 
				left join uu_land l on p.contact_id=l.id {$where}";
		$total = Db::fetch($total_sql);
		$total = $total['total'];
		if($req['for'] == 'distributor' && count($pids)){// 分销商分销了会员的分销产品
		    $pids = implode(',', $pids);
		    $sql="select pid,aids from `pft_p_apply_evolute` where sid=$sid and fid=$did and status=0 and pid in ($pids)";
		    $evolutes = Db::fetchAll($sql);
		    foreach ($evolutes as $row){
				$aids  = explode(',', $row['aids']);
				$count = count($aids);
				if(isset($aids[$count-2])){
					$aid = $aids[$count-2]; 
					$pk  = $row['pid'].'_'.$aid;
					if(!isset($result[$pk])) continue;
					$result[$pk]['allow'] = true;
					$p_storage = PFTCoreAPI::pStorage(
						$soap_client, $daccount, $row['pid'], $sid, $date, 0
					);
					$result[$pk]['settle_price'] = $p_storage['js']['p'];
				}
		    }
		} else if ($req['for'] == 'group') {
			$group_info = PriceGroup::getGroupById($req['gid'], $sid);
			if ($group_info['default_inc']) {
				$default_inc = json_decode($group_info['default_inc'], true);
				$allow_pids = array_keys($default_inc);
			} else {
				$allow_pids = array();
			}
			foreach ($result as $key => $item) {
				if (in_array($item['pid'], $allow_pids)) {
					$result[$key]['allow'] = true;
					$result[$key]['settle_price'] = $default_inc[$item['pid']] / 100;
				}
			}
		}
		$data = array();
		foreach ($result as $key => $item) {
			if (!isset($item['settle_price'])) {
				$item['settle_price'] = $item['supply_price'];
			}
			$item['key'] = $key;
			$data[] = $item;
		}
		$result = array(
			'data' => $data,
			'total_page' => ceil($total / $page_size),
			'current_page' => $page
		);
		ajaxReturn('productsList', $result);
		break;
	//获取景区及其所属门票
	
	case 'get_tickets':
		//如果传入的是pid,则先获取landid,再进一步获取下属的所有pid
		if (isset($req['pid']) && $req['pid'] > 0) {
			$land_sql = "select contact_id from uu_products where id={$req['pid']} limit 1";
			$landid = Db::fetch($land_sql);
			if (!$landid) {
				ajaxReturn('paramError');
			}
			$landid = $landid['contact_id'];
		} else {
			if (!isset($req['landid']) || $req['landid'] < 1) {
				ajaxReturn('paramError');
			}
		}
		$tickets_sql = "
			select p.id,t.title,p.p_name 
			from uu_jq_ticket t left join uu_products p on t.pid=p.id 
			where t.landid={$landid}";
		$tickets = Db::fetchAll($tickets_sql);
		$tmp = array();
		foreach ($tickets as $key => $ticket) {
			if ($ticket['id'] == $req['pid']) {
				$tmp = $ticket;
				unset($tickets[$key]);
				break;
			}
		}
		array_unshift($tickets, $tmp);
		if (!$tickets) {
			ajaxReturn('noSaleProduct');
		}
		$result = array(
			'land_info' => array(
				'landid' => $landid,
				'title' => str_replace($tickets[0]['title'], '', $tickets[0]['p_name'])
			),
			'tickets' => $tickets
		);
		ajaxReturn('ticketsList', $result);
		// var_dump($result);
		// $result

		break;
	//获取分销商分组
	case 'get_distributors':
		$distributors = get_distributors($sid);

		$did_arr = $new_distributors = array();
		foreach ($distributors as $distributor) {
			$did_arr[] = $distributor['id'];
			$new_distributors[$distributor['id']] = $distributor;
		}

		//将分销商按分组分类
		$own_groups = PriceGroup::getGroupsBySid($sid);
		$groups = array();
		foreach ($own_groups as $group) {
			$group_did_arr = explode(',', $group['dids']);
			$group_members = array();
			foreach ($group_did_arr as $did) {
				if (!in_array($did, $did_arr))
					continue;
				$group_members[] = $new_distributors[$did];
			}
			$groups[] = array(
				'group_id' => $group['id'],
				'gruop_name' => $group['name'],
				'group_members' => $group_members
			);
			$did_arr = array_diff($did_arr, $group_did_arr);
		}
		//未经分组的分销商，默认为“未分组”
		$unjoin_members = array();
		foreach ($did_arr as $did) {
			$unjoin_members[] = $new_distributors[$did];
		}
		$groups[] = array(
			'group_id' => 0,
			'group_name' => '未分组',
			'group_members' => $unjoin_members
		);
		ajaxReturn('distributorsList', $groups);
	//获取指定产品所在组的差价
	case 'get_default_settle_price':
		if (!isset($req['pid']) || $req['pid'] < 1) {
			ajaxReturn('paramError');
		}
		//type=0:自供应,type=1:转分销
		// $type = $req['type'] ? $req['type'] : 0;
		if (PriceGroup::isSelfProduct($req['pid'], $sid)) {
			$type = 0;
		} else {
			$type = 1;
		}
		$p_storage = PFTCoreAPI::pStorage(
			$soap_client, $_SESSION['saccount'], 
			$req['pid'], $sid, date('Y-m-d'),
			$type ? 0 : 2
		);
		if ($p_storage['js']['p'] < 0) {
			ajaxReturn('outOfDate');
		}
		$groups = PriceGroup::getGroupsBySid($sid);
		$result = array(
			'self' => array(
				'supply_price' => $p_storage['js']['p'],
				'recom_price' => $p_storage['ls']['p'],
			)
		);
		$distributors = get_distributors($sid);
		foreach ($distributors as $distributor) {
			$did_arr[] = $distributor['id'];
			$new_distributors[$distributor['id']] = $distributor;
			$account = Db::fetch("select account from pft_member where id={$distributor['id']} limit 1");
			$new_distributors[$distributor['id']]['account'] = $account['account'];
		}
		foreach ($groups as $group) {
			$price_set = json_decode($group['default_inc'], true);
			$result['group'][$group['id']]['price'] = null;
			if ($price_set) {
				if (isset($price_set[$req['pid']])) {
					$result['group'][$group['id']]['price'] = $price_set[$req['pid']] / 100;
				} else {
					$result['group'][$group['id']]['price'] = null;
				}
			}
			$group_dids = explode(',', $group['dids']);
			$result['group'][$group['id']]['group_name'] = $group['name'];
			$result['group'][$group['id']]['group_id'] = $group['id'];
			$result['group'][$group['id']]['supply_price'] = $result['self']['supply_price'];
			if ($group_dids) {
				$did_arr = array_diff($did_arr, $group_dids);
				foreach ($group_dids as $did) {
					$result['group'][$group['id']]['member'][] = $new_distributors[$did];
				}
			}
		}
		// $result['group'] = array_values($result['group']);
		// var_dump($result);die;
		// var_dump(array)
		//未分组直接获取当前的配置价格
		// $distributors = get_distributors($sid);
		// if (!$distributors) {
		// 	ajaxReturn('noResult');
		// }
		// foreach ($distributors as $distributor) {
		// 	$did_arr[] = $distributor['id'];
		// }
		// var_dump($did_arr);die;
		// $own_groups = PriceGroup::getGroupsBySid($sid);
		// foreach ($own_groups as $group) {
		// 	$group_did_arr = explode(',', $group['dids']);
		// 	// var_dump($did_arr);die;
		// 	$did_arr = array_diff($did_arr, $group_did_arr);
		// }
		$page = $req['page'] ?: 1;
		$page_size = $req['page_size'] ?: 10;
		$iterator = new ArrayIterator($did_arr);
		$limits = new LimitIterator($iterator, ($page - 1) * $page_size, $page_size);
		$tmp = array();
		foreach ($limits as $limit) {
		    $tmp[] = $limit;
		}
		$unjoin_priceset = get_price_for_distributor($sid, $tmp, $req['pid'], $type);
		// var_dump($unjoin_priceset);die;
		// $result['unjoin'] = $unjoin_priceset;
		$distributors = get_distributors($sid);
		foreach ($unjoin_priceset as $key => $mem) {
			$tmp = $new_distributors[$key];
			$tmp['price'] = $mem['price'];
			$tmp['allow'] = $mem['allow'];
			$result['group'][0]['group_id'] = 0;
			$result['group'][0]['member'][] = $tmp;

		}
		$result['group'] = array_values($result['group']);
		$result['current_page'] = $page;
		$result['total_page'] = ceil(count($did_arr) / $page_size);
		// var_dump($result);die;
		ajaxReturn('defaultPriceSet', $result);
		break;

	//设置指定产品在指定分组的差价
	case 'set_default_settle_price':
		if (!isset($req['data'], $req['pid']) || !$req['data'] || !$req['pid']) {
			ajaxReturn('paramError');
		}
		$groups = explode('|', $req['data']);
		foreach ($groups as $group) {
			list($gid, $settle_price) = explode(':', $group);
			if (!PriceGroup::belongTo($gid, $sid)) {
				ajaxReturn('paramError');
			}
			PriceGroup::setDefaultSettlePrice($gid, $req['pid'], $settle_price);
		}
		break;
	case 'setPrice':

		break;

	case 'disabled_product':
		if (!isset($req['pid']) || !$req['pid']) {
			ajaxReturn('paramError');
		}
		if ($req['for'] == 'distributor') {
			if (!isset($req['did']) || !$req['did']) {
				ajaxReturn('paramError');
			}
			$result = PriceGroup::disabledProductBydistributor($req['pid'], $req['did'], $sid);
		} else {
			if (!isset($req['gid']) || !$req['gid']) {
				ajaxReturn('paramError');
			}
			$result = PriceGroup::disabledProductByGroup($req['pid'], $req['gid'], $sid);
		}
		$result ? ajaxReturn("success") : ajaxReturn('fail');
		break;

	case 'savePriceForProduct':
		if (!isset($req['pid'], $req['distributor']) || !$req['pid']) {
			ajaxReturn('paramError');
		}
		if (PriceGroup::isSelfProduct($req['pid'], $sid)) {
			$result = PriceGroup::savePriceForProduct(
				$req['pid'], 
				$req['group'], 
				$req['distributor']
			);
		} else {
			if (!isset($req['sid']) || !$req['sid']) {
				ajaxReturn('paramError');
			}
			$result = PriceGroup::savePriceForProduct(
				$req['pid'],
				$req['group'], 
				$req['distributor'],
				$req['sid']
			);
		}
		
		if ($result) {
			ajaxReturn('success');
		} else {
			ajaxReturn('fail');
		}
		break;

	case 'savePriceForSelf':
		if (!isset($req['price'])) {
			ajaxReturn('success');
		} else {
			if (!$req['price']) {
				ajaxReturn('paramError');
			}
		}
		if ($req['type'] == 'group') {
			if (!isset($req['gid']) || !$req['gid']) {
				ajaxReturn('paramError');
			}
			$result = PriceGroup::savePriceForDistributorByGroup($req['gid'], $req['price'], $sid);
		} else {
			if (!isset($req['did']) || !$req['did']) {
				ajaxReturn('paramError');
			}
			$result = PriceGroup::savePriceForDistributorByDid($req['did'], $req['price']);
		}
		if ($result) {
			ajaxReturn('success');
		} else {
			ajaxReturn('fail');
		}
		break;

	case 'savePriceForDis':
		if (!isset($req['set']) || !$req['set']) {
			ajaxReturn('success');
		}
		if (!isset($req['type'], $req['src']) || !$req['type'] || !$req['src']) {
			ajaxReturn('paramError');
		}
		if ($req['type'] == 'group') {
			if (!isset($req['gid']) || !$req['gid']) {
				ajaxReturn('paramError');
			}
			$result = PriceGroup::savePriceForDisByGroup($req['gid'], $req['src'], $req['set'], $sid);
		} else {
			if (!isset($req['did']) || !$req['did']) {
				ajaxReturn('paramError');
			}
			$result = PriceGroup::savePriceForDisByDid($req['did'], $req['src'], $req['set']);
		}
		if ($result) {
			ajaxReturn('success');
		} else {
			ajaxReturn('fail');
		}
		break;
	default:
		exit('bad request');
}

//获取未分组的分销商列表
function get_distributors($sid) {
	global $req;
	if (isset($req['keyword']) && $req['keyword']) {
		$keyword = $req['keyword'];
		$key_condition = " and instr(m.dname,'".$keyword."') ";
	}

	$sql = "select m.id,m.dname,m.cname
		from pft_member as m left join pft_member_relationship as r  
		on m.id = r.son_id
		where m.status in (0,3) and r.status=0 and r.parent_id={$sid} and r.ship_type=0 
		and length(m.account)<11 and r.son_id<>{$sid}{$key_condition}";
		// echo $sql;die;
	return Db::fetchAll($sql);
}

function get_price_for_distributor($sid, $did_arr, $tid, $type = 0) {
	global $soap_client;
	$sql = "select pid,tid,dprice from uu_priceset where tid={$tid} and aid=0";
	$price_arr = Db::fetchAll($sql);
	$tmp = array();
	foreach ($price_arr as $price) {
		$tmp[$price['pid']] = $price['dprice'] / 100;
	}
	$price_arr = $tmp;
	$p_storage = PFTCoreAPI::pStorage(
		$soap_client, $_SESSION['saccount'], 
		$tid, $sid, date('Y-m-d'),
		$type ? 0 : 2
	);
	$result = array();
	foreach ($did_arr as $did) {
		if (isset($price_arr[$did])) {
			$result[$did]['price'] = $price_arr[$did] + $p_storage['js']['p'];
		} else {
			$result[$did]['price'] = $p_storage['js']['p'];
		}
	}
	if ($type == 0) {
		$sql = "select * from pft_product_sale_list where aid=$sid and status=0";
		$sales = Db::fetchAll($sql);
		// var_dump($sales);die;
		$tmp = array();
		foreach ($sales as $sale) {
			$tmp[$sale['fid']] = $sale;
		}
		$sales = $tmp;
		// var_dump($sales);die;
		foreach ($did_arr as $did) {
			if ($sales[$did]['pids']) {
				if ($sales[$did]['pids'] == 'A') {
					$result[$did]['allow'] = true;
					continue;
				}
				$pids = explode(',', $sales[$did]['pids']);
				if (in_array($tid, $pids)) {
					$result[$did]['allow'] = true;
				} else {
					$result[$did]['allow'] = false;
				}
			} else {
				$result[$did]['allow'] = false;
			}
		}
	} else {
		$sql = "select fid,pid,aids from `pft_p_apply_evolute` where sid=$sid and pid=$tid and status=0 ";
		$evolutes = Db::fetchAll($sql);
		$tmp = array();
		foreach ($evolutes as $evolute) {
			$tmp[$evolute['fid']] = $evolute;
		}
		$evolutes = $tmp;
		foreach ($did_arr as $did) {
			if (isset($evolutes[$did])) {
				$result[$did]['allow'] = true;
			} else {
				$result[$did]['allow'] = false;
			}
		}
		// var_dump($tmp);die;
	}
	//判断是否允许分销
	return $result;
}

// function get_price_for_distributor($sid, $did_arr, $tid, $type = 0) {
// 	global $soap_client;
// 	$sql = "select pid,tid,dprice from uu_priceset where tid={$tid} and aid=0";
// 	$price_arr = Db::fetchAll($sql);
// 	$tmp = array();
// 	foreach ($price_arr as $price) {
// 		$tmp[$price['pid']] = $price['dprice'] / 100;
// 	}
// 	$price_arr = $tmp;
// 	$p_storage = PFTCoreAPI::pStorage(
// 		$soap_client, $_SESSION['saccount'], 
// 		$tid, $sid, date('Y-m-d'),
// 		$type ? 0 : 2
// 	);
// 	$result = array();
// 	foreach ($did_arr as $did) {
// 		if (isset($price_arr[$did])) {
// 			$result[$did] = $price_arr[$did] + $p_storage['js']['p'];
// 		} else {
// 			$result[$did] = $p_storage['js']['p'];
// 		}
// 	}
// 	if ($type == 0) {
// 		$sql = "select * from pft_product_sale_list where aid=$sid and status=0";
// 		$sales = Db::fetchAll($sql);
// 		$tmp = array();
// 		foreach ($sales as $sale) {
// 			$tmp[$sale['fid']] = $sale;
// 		}
// 		$sales = $tmp;
// 		foreach ($did_arr as $did) {
// 			if ($sales[$did]['pids']) {
// 				$pids = explode(',', $sales[$did]['pids']);
// 				if (in_array($tid, $pids)) {
// 					continue;
// 				} 
// 			}
// 			unset($result[$did]);
// 		}
// 	} else {
// 		$sql = "select fid,pid,aids from `pft_p_apply_evolute` where sid=$sid and pid=$tid and status=0 ";
// 		$evolutes = Db::fetchAll($sql);
// 		// var_dump($evolutes);die;
// 		$tmp = array();
// 		foreach ($evolutes as $evolute) {
// 			$tmp[$evolute['fid']] = $evolute;
// 		}
// 		$evolutes = $tmp;
// 		foreach ($did_arr as $did) {
// 			if (isset($evolutes[$did])) {
// 				continue;
// 			} else {
// 				unset($result[$did]);
// 			}
// 		}
// 	}
// 	//判断是否允许分销
// 	return $result;
// }


function ajaxReturn($code, $info = null) {
	switch ($code) {
		case 'paramError':
			$status = 0;
			$info = '参数错误';
			break;
		case 'accountNotExists':
			$status = 0;
			$info = '账号不存在';
			break;
		case 'noSaleProduct':
			$status = 0;
			$info = '没有可售卖产品';
			break;
		case 'noProduct':
			$status = 0;
			$info = '没有发布产品';
			break;
		case 'outOfDate':
			$status = 0;
			$info = '产品已过期';
			break;
		case 'noResult':
			$status = 0;
			$info = '没有搜索到结果';
			break;
		case 'success':
			$status = 1;
			$info = '操作成功';
			break;
		case 'success':
			$status = 1;
			$info = '操作成功';
			break;
		case 'fail':
			$status = 0;
			$info = '操作失败';
			break;
		case 'ticketsList':
			$status = 1;
			break;
		case 'productsList':
			$status = 1;
			break;
		case 'distributorsList':
			$status = 1;
			break;
		case 'defaultPriceSet':
			$status = 1;
			break;
		default:
			exit();
	}
	exit(json_encode(
		array('status' => $status, 'code' => $code, 'info' => $info))
	);
}








// use pft\Prod\Product;
// use abc777\Syslog;
// use PFT\Db;


// include_once('common/autoload.php');
// include('module/common/Db.class.php');
// include_once('/var/www/html/new/d/class/abc/Syslog.class.php');

// if (!$_SESSION['memberID']) {
// 	exit(json_encode(array('status' => 2, 'info' => '请先登陆')));
// }

// $tpl = Tpl::getLink();

// $did = intval($_REQUEST["did"]);
// $sid = $_SESSION['sid'];
// $pri = $_REQUEST["pri"];
// $pid = $_REQUEST["pid"] ? $_REQUEST["pid"] : array();
// $tplid = $_REQUEST["tplid"];

// $_pri = array();
// if ($_REQUEST["bind"] && $tplid) {
// 	if (($res = $tpl->apply($did,$tplid)) === true) {
// 		exit(json_encode(array('status' => 1, 'info' => '配置成功')));
// 	} else {
// 		exit(json_encode(array('status' => 0, 'info' => $res)));
// 	}
// } else {
// 	foreach($pid as $v){
// 		//对价格进行限制
// 		if ($pri[$v] >= 10000) {
// 			exit(json_encode(
// 				array('status' => 0, 'info' => '价格配置必须限制在10000以内!'))
// 			);
// 		}
// 		$_pri[$v] = $pri[$v];
// 	}

// 	//请求日志
// 	Syslog::write_log(json_encode($_REQUEST), 'priceset');

// 	$pids = array_unique(array_keys($_pri));
// 	$update_sql = "update pft_product_sale_list set pids='".implode(',', $pids)."' where fid=".$did." and aid=".$sid;
// 	$update_result = Db::execute($update_sql);
// 	if ($update_result) {
// 		$log_str = $_SESSION['sid'] .'|'. $_SESSION['memberID'] .'|'. $update_sql;
// 		Syslog::write_log($log_str,'pft_product_sale_list');
// 	}
// 	if (!$_pri) {
// 		exit(json_encode(array('status' => 1, 'info' => '配置成功')));
// 	}

// 	$update_result = Db::execute(
// 		"update uu_fx_tpl_cs set tplid=0 where fxid=:did limit 1",
// 		 array(':did' => $did)
// 	);
// 	if (($res = $tpl->unusual($did, $_pri)) === true) {
// 		exit(json_encode(array('status' => 1, 'info' => '配置成功')));
// 	} else {
// 		exit(json_encode(array('status' => 0, 'info' => $res)));
// 	}
// }